package org.mq.mymq.broker.model;

import org.mq.mymq.broker.utils.ByteConverUtil;

/**
 * @ClassName ConsumerQueueDetailModel
 * @Description comsumerQueue数据结构存储的最小单元
 * @Author jiarong_ye
 * @Date 2025/6/16 15:34
 * @Version 1.0
 */
public class ConsumeQueueDetailModel {
    private int commitLogIndex; // 4byte
    private int msgIndex; // 4byte
    private int msgLength; //

    public int getCommitLogIndex() {
        return commitLogIndex;
    }

    public void setCommitLogIndex(int commitLogIndex) {
        this.commitLogIndex = commitLogIndex;
    }

    public int getMsgIndex() {
        return msgIndex;
    }

    public void setMsgIndex(int msgIndex) {
        this.msgIndex = msgIndex;
    }

    public int getMsgLength() {
        return msgLength;
    }

    public void setMsgLength(int msgLength) {
        this.msgLength = msgLength;
    }

    public byte[] converToBytes() {
        byte[] commitLogIndexs = ByteConverUtil.intToByteArray(commitLogIndex);
        byte[] msgIndexs = ByteConverUtil.intToByteArray(msgIndex);
        byte[] msgLengths = ByteConverUtil.intToByteArray(msgLength);
        byte[] finalBytes = new byte[commitLogIndexs.length + msgIndexs.length + msgLengths.length];
        System.arraycopy(commitLogIndexs, 0, finalBytes, 0, commitLogIndexs.length);
        System.arraycopy(msgIndexs, 0, finalBytes, commitLogIndexs.length, msgIndexs.length);
        System.arraycopy(msgLengths, 0, finalBytes, commitLogIndexs.length + msgIndexs.length, msgLengths.length);
        return finalBytes;
    }

    public void converFromBytes(byte[] content) {
        int commitLogIndex = ByteConverUtil.byteArrayToInt(ByteConverUtil.readInPos(content, 0, 4));
        int msgIndex = ByteConverUtil.byteArrayToInt(ByteConverUtil.readInPos(content, 4, 4));
        int msgLength = ByteConverUtil.byteArrayToInt(ByteConverUtil.readInPos(content, 8, 4));
        this.setCommitLogIndex(commitLogIndex);
        this.setMsgIndex(msgIndex);
        this.setMsgLength(msgLength);
    }

    @Override
    public String toString() {
        return "ConsumeQueueDetailModel{" +
                "commitLogIndex=" + commitLogIndex +
                ", msgIndex=" + msgIndex +
                ", msgLength=" + msgLength +
                '}';
    }
}
